package org.lq.er.view.scoreManege;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.lq.er.entity.Score_info;
import org.lq.er.entity.Student;
import org.lq.er.entity.Subject;
import org.lq.er.service.Score_infoService;
import org.lq.er.service.StudentService;
import org.lq.er.service.SubjectService;
import org.lq.er.service.impl.Score_infoServiceImpl;
import org.lq.er.service.impl.StudentServiceImpl;
import org.lq.er.service.impl.SubjectServiceImpl;
import org.lq.er.util.JDBCUtil;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class V_ScoreManage extends JFrame {

	private JPanel contentPane;
	private JTextField txtChoice;
	private final JScrollPane scrollPane = new JScrollPane();
	private JTable table;
    Score_infoService sis = new Score_infoServiceImpl() ;
    StudentService sts = new StudentServiceImpl();
    SubjectService sus = new SubjectServiceImpl();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_ScoreManage frame = new V_ScoreManage();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_ScoreManage() {
		setTitle("成绩管理");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 612, 477);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 10, 596, 44);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnNewButton = new JButton("显示全部");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				initTable(sis.findInfo());
			}
		});
		btnNewButton.setBounds(70, 10, 105, 23);
		panel.add(btnNewButton);
		
		JLabel lblNewLabel = new JLabel("班级/姓名/身份证号");
		lblNewLabel.setBounds(262, 14, 125, 15);
		panel.add(lblNewLabel);
		
		txtChoice = new JTextField();
		txtChoice.setBounds(384, 10, 105, 23);
		panel.add(txtChoice);
		txtChoice.setColumns(10);
		
		JButton btnSearch = new JButton("搜索");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String item = txtChoice.getText().trim();
				ResultSet rs = sis.findByItem(item);
				initTable(rs);
				txtChoice.setText("");
			}
		});
		btnSearch.setBounds(499, 10, 72, 24);
		panel.add(btnSearch);
		scrollPane.setBounds(0, 64, 596, 367);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"\u8EAB\u4EFD\u8BC1\u53F7", "\u59D3\u540D", "\u73ED\u7EA7", "\u79D1\u76EE", "\u662F\u5426\u7F3A\u8003", "\u6210\u7EE9"
			}
		));
		scrollPane.setViewportView(table);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("添加");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_ScoreAdd add = new V_ScoreAdd();
				add.setVisible(true);
				add.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						//当添加窗体关闭的时候,调用表格初始化方法
						initTable(sis.findInfo());
					}
					@Override
					public void windowClosing(WindowEvent e) {
					}
				});
				
			}
		});
		popupMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("删除");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//得到点击行的数据
				int rowIndex = table.getSelectedRow();
				String cardId = table.getValueAt(rowIndex, 0).toString();
				String subId_s = table.getValueAt(rowIndex, 3).toString().trim();
				Subject sub = sus.getBySubjectName(subId_s);
				if(sis.delete(cardId, sub.getId())) {
					JOptionPane.showMessageDialog(V_ScoreManage.this, "删除成功","提示",JOptionPane.INFORMATION_MESSAGE);
					initTable(sis.findInfo());
				}else {
					JOptionPane.showMessageDialog(V_ScoreManage.this, "删除失败","提示",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		popupMenu.add(mntmNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("修改");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//得到点击行的数据
				int rowIndex = table.getSelectedRow();
				String[] str = new String[6];
				for (int i = 0; i < str.length; i++) {
					str[i] = table.getValueAt(rowIndex, i).toString();
				}
				V_ScoreUpdate sud = new V_ScoreUpdate(str);
				sud.setVisible(true);
				sud.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						//当添加窗体关闭的时候,调用表格初始化方法
						initTable(sis.findInfo());
					}
					@Override
					public void windowClosing(WindowEvent e) {
					}
				});
			}
		});
		popupMenu.add(mntmNewMenuItem_2);
		initTable(sis.findInfo());
	}
	public void initTable(ResultSet rs) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int count = dtm.getRowCount(); //获取当前表的行
		// 清空表格
		for (int i = 0; i < count; i++) {
			dtm.removeRow(0);
		}
		 
		// 添加表格数据
		try {
			String flag;
			while(rs.next()) {
				if(rs.getInt(5)==1) {
					flag = "否";
				}else {
					flag = "是";
				}
				try {
					dtm.addRow(new Object[] {
						rs.getString(1),
						rs.getString(2),
						rs.getString(3),
						rs.getString(4),
						flag,
						rs.getInt(6)
					});
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
